RateLimit.constructor   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 45

Duplication

Lines 45
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 0
dl 45
loc 45
rs 8.8571

14 Functions

Rating   Name   Duplication   Size   Complexity  
A RateLimit.breakTimer 5 5 1
A RateLimit.startTimer 6 6 2
A RateLimit.setActive 3 3 1
A RateLimit.isActive 1 1 1
A RateLimit.getOriginalCalls 1 1 1
A RateLimit.getCallsLeft 1 1 1
A RateLimit.reset 4 4 1
A RateLimit.getOriginalTime 1 1 1
A RateLimit.decrementCall 1 1 1
A RateLimit.decrementTime 1 1 1
A RateLimit.getTimeLeft 1 1 1
A ratelimit.js ➔ RateLimit 9 9 3
A RateLimit.restartTimer 4 4 1
A RateLimit.startTimer 10 10 1
1 View Code Duplication
"use strict";
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
Object.defineProperty(exports, "__esModule", { value: true });
3
var RateLimit = /** @class */ (function () {
4
    function RateLimit(calls, time, callsLeft, timeLeft) {
5
        if (callsLeft === void 0) { callsLeft = calls; }
0 ignored issues
show
Coding Style introduced by
Consider using undefined instead of void(0). It is equivalent and more straightforward to read.
Loading history...
6
        if (timeLeft === void 0) { timeLeft = time; }
0 ignored issues
show
Coding Style introduced by
Consider using undefined instead of void(0). It is equivalent and more straightforward to read.
Loading history...
7
        this.originCalls = calls;
8
        this.originTime = time;
9
        this.currentCalls = callsLeft;
10
        this.currentTime = timeLeft;
11
        this.active = false;
12
    }
13
    RateLimit.prototype.reset = function () {
14
        this.currentCalls = this.originCalls;
15
        this.currentTime = this.originTime;
16
    };
17
    RateLimit.prototype.decrementCall = function () { this.currentCalls--; };
18
    RateLimit.prototype.decrementTime = function () { this.currentTime--; };
19
    RateLimit.prototype.getOriginalCalls = function () { return this.originCalls; };
20
    RateLimit.prototype.getOriginalTime = function () { return this.originTime; };
21
    RateLimit.prototype.getCallsLeft = function () { return this.currentCalls; };
22
    RateLimit.prototype.getTimeLeft = function () { return this.currentTime; };
23
    RateLimit.prototype.isActive = function () { return this.active; };
24
    RateLimit.prototype.setActive = function (active) {
25
        this.active = active;
26
    };
27
    RateLimit.prototype.startTimer = function () {
28
        var _this = this;
29
        this.setActive(true);
30
        this.timer = setInterval(function () {
31
            _this.decrementTime();
32
            if (_this.getTimeLeft() <= 0) {
33
                _this.breakTimer();
34
            }
35
        }, 1000);
36
    };
37
    RateLimit.prototype.breakTimer = function () {
38
        this.reset();
39
        this.setActive(false);
40
        clearInterval(this.timer);
41
    };
42
    RateLimit.prototype.restartTimer = function () {
43
        this.breakTimer();
44
        this.startTimer();
45
    };
46
    return RateLimit;
47
}());
48
exports.RateLimit = RateLimit;
49
//# sourceMappingURL=ratelimit.js.map